<template>
    <div>
        <el-form :inline="true" :model="form" class="demo-form-inline">
            <el-form-item>
                <el-input clearable v-model="form.title" style="width: 300px" placeholder="议事标题"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="query">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="success" @click="creat">新增议事</el-button>
            </el-form-item>
        </el-form>
        <!--        协商议事列表页面-->
        <el-table :data="tableData" border>
            <el-table-column prop="title" align="center" label="议事标题"></el-table-column>
            <el-table-column prop="time" align="center" label="议事时间"></el-table-column>
            <el-table-column prop="host" align="center" label="主持人"></el-table-column>
            <el-table-column prop="content" align="center" label="议事内容"></el-table-column>
            <el-table-column prop="address" align="center" label="议事地址"></el-table-column>
            <el-table-column align="center" label="操作">
                <template slot-scope="scope">
                    <el-button
                            type="success"
                            size="mini"
                            @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button
                            type="danger"
                            size="mini"
                            @click="handleDelete(scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNo"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next"
                :total="totalRecord" class="text-center mt10">
        </el-pagination>
<!--        创建弹出框-->
        <el-dialog title="创建" :visible.sync="dialogFormVisible">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm"  label-width="180px"
                     class="demo-ruleForm">
                <el-form-item label="议事标题" prop="title">
                    <div style="width: 320px">
                        <el-input v-model="ruleForm.title"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="议事时间" prop="time">
                    <div style="width: 320px">
                        <el-input v-model="ruleForm.time"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="主持人" prop="host">
                    <div style="width: 320px">
                        <el-input v-model="ruleForm.host"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="议事内容" prop="content">
                    <div style="width: 320px">
                        <el-input v-model="ruleForm.content"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="议事地址" prop="address">
                    <div style="width: 320px">
                        <el-input v-model="ruleForm.address"></el-input>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="yes">确 定</el-button>
            </div>
        </el-dialog>
<!--        编辑弹出框-->
        <el-dialog title="编辑" :visible.sync="dialogFormVisibleEdit">
            <el-form :model="ruleFormEdit" :rules="rulesEdit" ref="ruleForm"  label-width="180px"
                     class="demo-ruleForm">
                <el-form-item label="议事标题" prop="title">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.title"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="议事时间" prop="time">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.time"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="主持人" prop="host">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.host"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="议事内容" prop="content">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.content"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="议事地址" prop="address">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.address"></el-input>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
                <el-button type="primary" @click="sure">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>


<script>
    export default {
        data() {
            return {
                form: {
                   title:'' //议事标题
                },
                formLabelWidth: '150px',
                tableData: [],// 待审批数据表
                pageNo: 1,
                pageSize: 10,
                totalRecord: 0,
                totalPage: 0,
                dialogFormVisible: false, //创建弹出框
                dialogFormVisibleEdit: false, //编辑弹出框
                ruleForm: {},
                ruleFormEdit:{},
                rules: {   //创建验证
                    title: [
                        {required: true, message: '请输入议事标题', trigger: 'blur'},
                        // {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ],
                    time: [
                        {required: true, message: '请输入议事时间', trigger: 'blur'},
                        // {min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
                    ],
                    host: [
                        {required: true, message: '请输入主持人', trigger: 'blur'},
                        // {min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
                    ],
                    content: [
                        {required: true, message: '请输入议事内容', trigger: 'blur'},
                        // {min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
                    ],
                    address: [
                        {required: true, message: '请输入议事地址', trigger: 'blur'},
                        // {min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
                    ],


                },
                rulesEdit: {   //编辑验证
                    title: [
                        {required: true, message: '请输入议事标题', trigger: 'blur'},
                        // {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ],
                    time: [
                        {required: true, message: '请输入议事时间', trigger: 'blur'},
                        // {min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
                    ],
                    host: [
                        {required: true, message: '请输入主持人', trigger: 'blur'},
                        // {min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
                    ],
                    content: [
                        {required: true, message: '请输入议事内容', trigger: 'blur'},
                        // {min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
                    ],
                    address: [
                        {required: true, message: '请输入议事地址', trigger: 'blur'},
                        // {min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
                    ],


                },
                cid:''
            }
        },

        methods: {
            //初始化
            async init() {
                const res = await this.$http('agendalist',{
                    pageNo: this.pageNo,
                    pageSize: this.pageSize
                });
                console.log(res)
                this.tableData = res.results
                this.totalPage = res.totalPage;
                this.totalRecord = res.totalRecord;
            },
            //创建
            creat(){
              this.dialogFormVisible= true
            },
            //点击确定创建
            async yes(){
                const  res = await this.$http('agendaCreat',{
                    title:this.ruleForm.title,
                    time:this.ruleForm.time,
                    host:this.ruleForm.host,
                    content:this.ruleForm.content,
                    address:this.ruleForm.address
                })
                if (res.count === 1) {
                    this.$message({
                        message: '创建成功',
                        type: 'success'
                    });
                    this.dialogFormVisible = false
                    this.init()
                } else {
                    this.$message({
                        message: '创建失败',
                        type: 'error'
                    });
                }
            },
            //删除
            async handleDelete(row){
                this.$confirm('将永久删除该内容, 是否继续?', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const res =await this.$http('agendaCreat',{
                        id:row.id,
                        del_flag:'1'
                    })
                    this.init()
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //编辑
            handleEdit(row){
                this.dialogFormVisibleEdit= true
                this.ruleFormEdit = row
                this.cid = row.id
            },
            //点击确定完成编辑

            async sure(){
                const  res = await this.$http('agendaCreat',{
                    id:this.cid,
                    title:this.ruleFormEdit.title,
                    time:this.ruleFormEdit.time,
                    host:this.ruleFormEdit.host,
                    content:this.ruleFormEdit.content,
                    address:this.ruleFormEdit.address
                })
                if (res.count === 1) {
                    this.$message({
                        message: '编辑成功',
                        type: 'success'
                    });
                    this.dialogFormVisibleEdit = false
                    this.init()
                } else {
                    this.$message({
                        message: '编辑失败',
                        type: 'error'
                    });
                }
            },
            handleClick(tab, event) {
                console.log(tab, event);
            },
            //分页
            async handleSizeChange(val) {
                this.pageSize = val;
                this.init();
            },
            async handleCurrentChange(val) {
                this.pageNo = val;
                this.init();
            },
            //搜索
            async query() {
                const res = await this.$http('agendalist', {
                    title: this.form.title,
                });
                this.tableData = res.results
                this.totalPage = res.totalPage;
                this.totalRecord = res.totalRecord;
            },

        },

        watch: {},

        mounted() {
            this.init();
        },

    }
</script>


<style scoped>
    .w400 {
        width: 330px
    }
</style>
